package collections;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;

/**
 * 双向映射，并强制保证value的唯一性
 * 
 * @author tiangh
 * 2013-5-26 下午8:18:22
 */
public class BiMapDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		translate();
	}
	
	public static void translate() {
		BiMap<String,String> ch2Eng = HashBiMap.create();
		ch2Eng.put("传统", "traditional");
		ch2Eng.put("同步", "sync");
		ch2Eng.put("映射", "map");
		ch2Eng.put("混乱", "confuse");
		ch2Eng.put("值", "value");
		ch2Eng.put("独立的", "separate");
		ch2Eng.put("保持", "maintain");
		System.out.println(ch2Eng.get("映射"));
		
		//java.lang.IllegalArgumentException: value already present: map
//		ch2Eng.put("地图", "map");
//		如果要修改已经存在value的key那么必须这么做
		ch2Eng.forcePut("地图", "map");
		System.out.println(ch2Eng);
		
		BiMap<String, String> eng2Ch = ch2Eng.inverse();
		System.out.println(eng2Ch.get("maintain"));
	}
	
}
